import { View, Text} from "@tarojs/components"
import moment from "moment";


//用于中文日期的显示，
const Date = () => {
    // 日期的处理（汉化）
    const chinese = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
    const len = ['十'];
    const ydm = ['年', '月', '日'];
    const num2chinese = (s) => {
        //将单个数字转成中文.
        s = "" + s;
        let slen = s.length;
        let result = "";
        for (var i = 0; i < slen; i++) {
            result += chinese[s.charAt(i)];
        }
        return result;
    }

    const n2c = (s) => {
        //对特殊情况进行处理.    
        s = "" + s;
        // let result = "";
        if (s.length == 2) {
            if (s.charAt(0) == "1") {
                if (s.charAt(1) == "0") return len[0];
                return len[0] + chinese[s.charAt(1)];
            }
            if (s.charAt(1) == "0") return chinese[s.charAt(0)] + len[0];
            return chinese[s.charAt(0)] + len[0] + chinese[s.charAt(1)];
        }
        return num2chinese(s)
    }

    const baodate2chinese = (s) => {
        //验证输入的日期格式.并提取相关数字.    
        const datePat = /^(\d{2}|\d{4})(\/|-)(\d{1,2})(\2)(\d{1,2})$/;
        const matchArray = s.match(datePat);
        let ok = "";
        if (matchArray == null) return false;
        for (let i = 1; i < matchArray.length; i = i + 2) {
            ok += n2c(matchArray[i] - 0) + ydm[(i - 1) / 2];
        }
        return ok;
    }


    return (
        <View>
            <Text className='dataText'>
                {/* 中文日期的展示 */}
                {
                     baodate2chinese(moment().format('YYYY-MM-DD')) 
                }
            </Text>
        </View>
    )
}
export default Date